from random import random

import jwt
class Myjwt:
    def __init__(self):
        # 密钥
        self.secret_key = "Abcd#$%^&*(^##$"
    # 生成token
    def jwt_encode(self, data):
        return jwt.encode(data, self.secret_key, algorithm='HS256')
    # 解析token
    def jwt_decode(self, token):
        try:
            payload = jwt.decode(token, self.secret_key, algorithms=['HS256'])
        except:
            payload = None
        return payload
    # 对比token
    def check_token(self, token):
        # 解析payload
        payload = self.jwt_decode(token)
        # 用payload重新生成token
        newtoken = self.jwt_encode(payload)
        if token == newtoken:
            return True
        return False

mjwt = Myjwt()

